﻿using AppService.IService;
using AppService.IService.System_setting;
using AppService.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace WebAPI.Controllers.Login
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {

        private readonly IOptions<JWTOptions> options;

        public LoginController(IOptions<JWTOptions> options)
        {
            this.options = options;
        }

        public IEmployeeService employeeService { get; set; }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="uname">用户名</param>
        /// <param name="pass">密码</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetLogin(string uname, string pass)
        {
            try
            {
                var emp = employeeService.GetLogin(uname, pass);
                if (emp != null)
                {
                    var claims = new List<Claim>();
                    claims.Add(new Claim(ClaimTypes.Sid, emp.Emp_id.ToString()));
                    claims.Add(new Claim(ClaimTypes.Name, emp.Emp_name));
                    string jwtToken = BuildToken(claims, options.Value);
                    Console.WriteLine(jwtToken);
                    return Ok(new
                    {
                        code = 1,
                        message = "登录成功",
                        token = jwtToken,
                        value = emp
                    });
                }
                else
                {
                    return Ok(new
                    {
                        code = -1,
                        message = "用户名或密码错误"
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("异常信息：" + ex.Message);
                return Ok(new
                {
                    code = -1,
                    message = "异常错误"
                });
            }
        }

        /// <summary>
        /// 创建token
        /// </summary>
        /// <param name="claims">集合对象</param>
        /// <param name="options">配置对象</param>
        /// <returns></returns>
        private static string BuildToken(IEnumerable<Claim> claims, JWTOptions options)
        {
            DateTime expires = DateTime.Now.AddSeconds(options.ExpireSeconds);
            //读取加密算法
            SymmetricSecurityKey secKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(options.SigningKey));
            //指定加密算法
            SigningCredentials credentials = new SigningCredentials(secKey, SecurityAlgorithms.HmacSha256Signature);
            //创建jwt
            JwtSecurityToken tokenDescriptor = new JwtSecurityToken(expires: expires, signingCredentials: credentials, claims: claims);
            return new JwtSecurityTokenHandler().WriteToken(tokenDescriptor);
        }

    }
}
